package org.vgou.dao;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import java.sql.Connection;

import java.sql.Timestamp;
import java.io.Serializable;
import java.util.Date;

/**
* 
* Copyright (c) 2011, vgou<br>
* All rights reserved.<br>
* 
* @since 2011-11-27
* @author <a href="mailto:jason.hxx@gmail.com">jason</a>
* @version Id: IBaseDao.java 
* @comment	数据库接口类
* 
*/
public interface IBaseDao {

	public Object loadByPriKey(final Class<?> entity, final Serializable id);

	public int getCountByCriteria(final DetachedCriteria detachedCriteria);

	public List findByCriteria(final DetachedCriteria detachedCriteria,
			final int pageIndex, final int pageSize);

	public Criteria getCriteriaByDetachedCriteria(
			final DetachedCriteria detachedCriteria);

	public Criteria getCriteria(Class<?> clazz) throws DataAccessException;

	public Object getByPk(Class<?> clazz, Integer id)
			throws DataAccessException;

	public Object getByPk(Class<?> clazz, Long id) throws DataAccessException;

	public Object getByPk(Class<?> clazz, String id) throws DataAccessException;

	/**
	 * 根据条件取得实体类
	 * 
	 * @param entity
	 * @param key
	 * @param value
	 * @return
	 */
	public Object getEntityByArgs(Class<?> entity, Map<String, Object> args);

	/**
	 * 通过一个对象查找,该对象可以是一个复合键
	 * 
	 * @param entity
	 *            Class
	 * @param object
	 *            Serializable
	 * @return Object
	 * @throws DataAccessException
	 */
	public Object getByPk(Class<?> entity, Serializable object)
			throws DataAccessException;

	public Object create(Object entity) throws DataAccessException;

	/**
	 * 
	 */
	public void saveOrUpdate(Object entity) throws DataAccessException;

	public void batchSave(List objectsToSave);

	public void batchUpdate(List objectsToUpdate);

	public void batchSaveOrUpdate(List objectsToSaveOrUpdate);

	public void batchRemove(List objectsToRemove);

	/**
	 * 批量删除数据
	 * 
	 * @param c
	 *            需要删除的表类
	 * @param key
	 *            删除的字段依据
	 * @param values
	 *            删除的值，数组，使用英文逗号隔开
	 */
	public void batchRemoveByKey(Class<?> c, String key,
			Object[] values);
	
	public void update(Object entity) throws DataAccessException;

	public void merge(Object entity) throws DataAccessException;

	public void refresh(Object entity) throws DataAccessException;

	public Session getHibernateSession();

	/**
	 * SQL更新操作
	 * 
	 * @param allEntity
	 *            List
	 */
	public boolean runSqlSaveOrUpdate(final String hql)
			throws DataAccessException;

	/**
	 * SQL查询操作
	 * 
	 * @param allEntity
	 *            List
	 */
	public Object runSqlQuery(final String hql) throws DataAccessException;

	public void delete(Object entity) throws DataAccessException;

	public void deleteAll(Class clazz) throws DataAccessException;

	public void deleteAll(Collection entities) throws DataAccessException;

	public Object loadByKey(Class clazz, String keyName, Object keyValue)
			throws DataAccessException;

	public List loadAll(Class clazz) throws DataAccessException;

	public Object loadByPK(Class clazz, String id) throws DataAccessException;
	
	public Object loadByIDAndPK(Class clazz, long topicID,long userPK)throws DataAccessException;

	public List find(String query);

	public List findByHql(String hql) throws DataAccessException;

	public List getTotal(String hql, Date startDate) throws DataAccessException;

	public int getCount(String queryString) throws DataAccessException;

	public List findByNamedQuery(String namedQuery) throws DataAccessException;

	/**
	 * 获取前几个数据
	 */
	public List getLimit(final int count, final String hql);

	/**
	 * 抓取的结果集
	 * 
	 * @param count
	 * @param hql
	 * @return
	 */
	public List getFetchSize(final int count, final String hql);

	/**
	 * 分页返回查询结果，如果分页不正确，则不能返回数据。
	 * 
	 * @param startRs
	 *            ，开始页，第一页为0
	 * @param perPage
	 *            ，每页显示的条目数。
	 * @param hql
	 *            ，查询语句
	 * @return 返回列表 List
	 */
	public List getPageList(int startRs, int perPage, String hql)
			throws DataAccessException;

	/**
	 * 分页返回查询结果，如果分页不正确，则不能返回数据。
	 * 
	 * @param startRs
	 *            ，开始页，第一页为0
	 * @param perPage
	 *            ，每页显示的条目数。
	 * @param hql
	 *            ，查询语句
	 * @return 返回列表 List
	 */
	public List getPageList(int startRs, int perPage, String hql, Date startDate)
			throws DataAccessException;

	public List getPageListByTime(int startRs, int perPage, String hql,
			Timestamp beginTime, Timestamp endTime);

	/**
	 * 执行HQL语句进行创建或更新或删除
	 * 
	 * @param hql
	 *            String
	 * @return int
	 */
	public int runHql(String hql);

	/**
	 * 得到数据库连接
	 * 
	 * @param allEntity
	 *            List
	 */
	public Connection getConnection() throws DataAccessException;

	/**
	 * 通过开始时间和结束时间得到数据总和
	 * 
	 * @param hql
	 * @param startDateConditions
	 *            开始时间条件，即pojo的字段
	 * @param startDate
	 *            开始时间的值
	 * @param endDateConditions
	 *            结束时间条件
	 * @param endDate
	 *            结束时间值
	 * @return
	 */
	public List getBetweenTimeTotalCount(final String hql,
			final String startDateConditions, final Date startDate,
			final String endDateConditions, final Date endDate);

	/**
	 * 执行HQL查询,使用格式 from Object a where a.id:id
	 * 
	 * 
	 * @param queryString
	 * @param parameters
	 * @return
	 */
	public List findByQuery(String queryString, String[] fields,
			Object[] parameters);

}
